package sein.android.hopenote;

import sein.android.hopenote.Const.HopeNoteDb;
import android.R.string;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class User_Update extends Activity {

	private DBAdapter HnopenHelper;
	private SQLiteDatabase sdb;
	private ContentValues u_values;
	private Cursor cursor;
	private String User, sex, Age, Search;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		HnopenHelper = new DBAdapter(this);
		sdb = HnopenHelper.getReadableDatabase();
		setContentView(R.layout.user_update);

		final Button btn_Update = (Button) findViewById(R.id.btn_Udate);
		final EditText txt_Name = (EditText) findViewById(R.id.et_Name);
		final DatePicker dPk_Birthday = (DatePicker) findViewById(R.id.dp_Birthday);
		final RadioGroup SexGroup = (RadioGroup) findViewById(R.id.rg_Sex);
		// final RadioButton Sex
		// =(RadioButton)findViewById(SexGroup.getCheckedRadioButtonId());
		final RadioButton r_Man = (RadioButton) findViewById(R.id.r_Man);
		final RadioButton r_Woman = (RadioButton) findViewById(R.id.r_Woman);

		txt_Name.setText(HopeNoteDb.MODIFY_USER);
		cursor = sdb.query(HopeNoteDb.TB_USER, new String[] {
				HopeNoteDb.COL_USER, HopeNoteDb.COL_SEX, HopeNoteDb.COL_AGE },
				HopeNoteDb.COL_USER + " = '" + txt_Name.getText().toString()
						+ "'", null, null, null, null);

		cursor.moveToFirst();

		if (cursor.getString(1).equals("0")) {
			r_Man.setChecked(true);
		} else {
			r_Woman.setChecked(true);

		}

		String year = cursor.getString(2).substring(0, 4);
		String monthOfYear = cursor.getString(2).substring(5, 7);
		String dayOfMonth = cursor.getString(2).substring(8, 10);

		dPk_Birthday.updateDate(Integer.valueOf(year),
				Integer.valueOf(monthOfYear) - 1, Integer.valueOf(dayOfMonth));

		cursor.close();

		Log.i("lllll", "pl");

		btn_Update.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				dPk_Birthday.clearFocus();
				HnopenHelper = new DBAdapter(getApplicationContext());
				sdb = HnopenHelper.getWritableDatabase();
				if (txt_Name.getText().toString().replace(" ", "").equals("")) {
					Toast.makeText(User_Update.this, "사용자를 입력하세요.",
							Toast.LENGTH_LONG).show();
				} else {

					RadioButton Sex = (RadioButton) findViewById(SexGroup
							.getCheckedRadioButtonId());
					String sBirthDay = null;
					String sSex = null;

					String mon = Integer.toString(dPk_Birthday.getMonth() + 1);
					if (mon.length() == 1) {
						mon = "0" + mon;
					}
					String day = Integer.toString(dPk_Birthday.getDayOfMonth());
					if (day.length() == 1) {
						day = "0" + day;
					}
					sBirthDay = Integer.toString(dPk_Birthday.getYear()) + "-"
							+ mon + "-" + day;
					// Log.i("BirthDay", sBirthDay);

					if (Sex.getText().toString().trim().equals("남")) {
						sSex = "0";
					} else {
						sSex = "1";
					}
					User = txt_Name.getText().toString();
					sex = sSex;
					Age = sBirthDay;

					u_values = new ContentValues();
					u_values.put(HopeNoteDb.COL_USER, User);
					u_values.put(HopeNoteDb.COL_SEX, sex);
					u_values.put(HopeNoteDb.COL_AGE, Age);

					String Where = HopeNoteDb.COL_USER + " = '"
							+ HopeNoteDb.MODIFY_USER + "'";

					if (sdb.update(HopeNoteDb.TB_USER, u_values, Where, null) > 0)
					{
						if (HopeNoteDb.CURRENT_USER.equals(HopeNoteDb.MODIFY_USER)) {
							HopeNoteDb.CURRENT_USER = User;
							
							// send Broadcast
							Intent intent = new Intent("Reset");
							sendBroadcast(intent);

							Intent intent_main = new Intent("BR_Main");
							sendBroadcast(intent_main);

							Intent intent_noteadd = new Intent("BR_NoteAdd");
							sendBroadcast(intent_noteadd);

							SharedPreferences cache = getSharedPreferences("cache", Activity.MODE_PRIVATE);
							SharedPreferences.Editor editor = cache.edit();
							editor.putString("USER", User);
							editor.commit();
						}

						Toast.makeText(User_Update.this, "사용자가 수정 되었습니다.", Toast.LENGTH_LONG).show();
					} else {
						Toast.makeText(User_Update.this, "저장 실패", Toast.LENGTH_LONG).show();
					}

					// reset user list
					User_List_Adapter.Reset();
					Intent intent = new Intent("UserListReset");
					sendBroadcast(intent);
					finish();
				}
			}

		});
	}

	protected void notifyDataSetChanged() {
		// TODO Auto-generated method stub

	}
}
